package com.orhon.smartcampus.modules.teacher.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.teacher.dao.PaperDao;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.entity.Paper;
import com.orhon.smartcampus.modules.teacher.gqlInputModel.GQLPaperInput;
import com.orhon.smartcampus.modules.teacher.inputMapper.PaperInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddPaperMutation implements DataFetcher {

    @Autowired
    PaperDao paperDao;

    @Autowired
    TInformationDao tInformationDao;

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    TokenService tokenService;

    /**
     * 添加论文信息
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        GQLPaperInput input = PaperInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        Paper paper = null;

        if (input.getId() != null) {
            paper = paperDao.get(Paper.class, input.getId());
            paper.setUpdatedAt(new Date());
        } else {
            paper = new Paper();
            paper.setCreatedAt(new Date());
        }
        if(schoolId != null){

            paper.setSchool_id(schoolId);
        }
        if(input.getTeacher_id() != null){

            paper.setTeacher_id(input.getTeacher_id());
        }
        if(input.getCore() != null){

            paper.setCore(input.getCore());
        }
        if(input.getIsinclude() != null){

            paper.setIsinclude(input.getIsinclude());
        }
        if(input.getAuthornumber() != null){

            paper.setAuthornumber(input.getAuthornumber());
        }
        if(input.getPublishdate() != null){

            paper.setPublishdate(input.getPublishdate());
        }
        if(input.getImage() != null){

            paper.setImage(input.getImage());
        }
        if(input.getShenhe() != null){

            paper.setShenhe(input.getShenhe());
        }
        if(input.getTitle() != null){

            paper.setTitle(JSON.toJSONString(input.getTitle()));
        }
        if(input.getProduction() != null){

            paper.setProduction(JSON.toJSONString(input.getProduction()));
        }

        try{
            paperDao.saveOrUpdate(paper);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

